var app = angular.module("newApp", []),
	parid=UI.getParameterByName('parId');

var timeout_json=function(data,$window){ 
	if (data.returnCode == 999994) {
				var ss = 3;
				layer.msg(data.returnMessage + "！<strong class='Ajax_Out'>" + ss + "</strong> 秒后前往登录页", 50, -1);
				var picInt = setInterval(function() {
					if (ss >= 1) {
						ss = ss - 1;
						$('.Ajax_Out').text(ss);
					} else {
						clearInterval(picInt);
						try{
							$window.parent.location.href = data.returnData;
							$window.location.href = data.returnData;
						}catch(e){ 
							log('timeout!')
						}
					}
				}, 1000);
		return false;
	}
}

app.config(function($httpProvider) {
    $httpProvider.defaults.headers.put['Content-Type'] = 'application/x-www-form-urlencoded';
    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
	$httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
 
    // Override $http service's default transformRequest
    $httpProvider.defaults.transformRequest = [function(data) {
        /**
         * The workhorse; converts an object to x-www-form-urlencoded serialization.
         * @param {Object} obj
         * @return {String}
         */
        var param = function(obj) {
            var query = '';
            var name, value, fullSubName, subName, subValue, innerObj, i;
 
            for (name in obj) {
                value = obj[name];
 
                if (value instanceof Array) {
                    for (i = 0; i < value.length; ++i) {
                        subValue = value[i];
                        fullSubName = name + '[' + i + ']';
                        innerObj = {};
                        innerObj[fullSubName] = subValue;
                        query += param(innerObj) + '&';
                    }
                } else if (value instanceof Object) {
                    for (subName in value) {
                        subValue = value[subName];
                        fullSubName = name + '[' + subName + ']';
                        innerObj = {};
                        innerObj[fullSubName] = subValue;
                        query += param(innerObj) + '&';
                    }
                } else if (value !== undefined && value !== null) {
                    query += encodeURIComponent(name) + '='
                            + encodeURIComponent(value) + '&';
                }
            }
            return query.length ? query.substr(0, query.length - 1) : query;
        };
 
        return angular.isObject(data) && String(data) !== '[object File]'
                ? param(data)
                : data;
    }];
});

app.controller('newCtrl', function($scope, $http,$window,$timeout) {
	$http.post(basePath + 'rpt/view/select.do').success(function(data) {
		content = $scope.content = data.returnData;
	});
	var vm = $scope.vm = {};
	$scope.domAjax = function(e,id) {
		if(id){
			if(e==="daily"){
				$http.get(basePath + 'rpt/view/queryByGidCid.do?groupId='+id+'&cycleId=1').success(function(data) {
					if(data.returnData>0){
						layer.msg("该组织已拥有日报模板!",2,5);
					}else{
						iframeFunc("日报",0);
					}
				});
			}else if(e==="weekly"){
				$http.get(basePath + 'rpt/view/queryByGidCid.do?groupId='+id+'&cycleId=2').success(function(data) {
					if(data.returnData>0){
						layer.msg("该组织已拥有周报模板!",2,5);
					}else{
						iframeFunc("周报",1);
					}
				});
				
			}else if(e==="monthly"){
				$http.get(basePath + 'rpt/view/queryByGidCid.do?groupId='+id+'&cycleId=3').success(function(data) {
					if(data.returnData>0){
						layer.msg("该组织已拥有月报模板!",2,5);
					}else{
						iframeFunc("月报",2);
					}
				});
			}
			
			function iframeFunc(rptTypeName,rptType){
				var maxH=$(window).height()*0.9<760?760:$(window).height()*0.9;
					maxW=$(window).width()*0.9>1200?1200:$(window).width()*0.9;
				var t=$(this);
				var osf=$.layer({
			        type : 2,
			        title: '新增'+rptTypeName+'模板',
			        shadeClose: false,
			        maxmin: false,
			        fix : false,  
			        area: [maxW, maxH],                     
			        iframe: {
			            src : basePath + 'report/reportTemplateNewcreates.jsp?parId=3&groupId=' + id + '&rptType=' + rptType
			        } 
		   	 	});
			}
		}else{
			layer.msg("请选择所属组织！",1,5)	;
		}
	};
});

app.directive('agSelectd', function($http){
	return { 
		restrict:'AE',
		scope:{ 
			groupId:'=groupId'
		},
		template:'<div class="ui_select"><input type="text" id="dailyGroupInputText" class="ui_s_t" ng-bind="stxt" ng-model="stxt" /><div class="ui_s_bk"><a href="javascript:void(0)" ng-repeat="key in dataGroupSelects | filter: {groupName:stxt}" ng-bind="key.groupName" data-id="{{key.groupId}}"></a></div></div>',
		controller:function($scope,$element,$attrs){ 
			var a = $element.find('.ui_s_t'),
				b = $element.find('.ui_s_bk'),
				c = $attrs.ajaxs,
				d = $attrs.loadajaxs;
			$element.children('.ui_select').css('zIndex',102);
			$element.bind('click', function() {
				b.slideDown();
			});
			$element.bind('mouseleave', function() {
				if(a.text()!=""){
					b.hide();
				}
			});
		},
		link:function($scope,$element,$attrs){ 
			var a=$element.find('.ui_s_t'),
				b=$element.find('.ui_s_bk'),
				c=$attrs.ajaxs;
			if(c!=undefined){
				$http.get(c+'?vtimes='+new Date().getTime()).success(function(data){ 
					$scope.dataGroupSelects=data.returnData;
				});
			}			
			a.click(function() {
				$scope.stxt=undefined;
				$scope.$apply();
			});
			b.find('a').live("click",function(){ 
				b.hide();
				$(this).addClass('cur').siblings().removeClass('cur');
				a.val($(this).text());
				$scope.stxt=$(this).text();
				if($attrs.groupId!=undefined) {
					$scope.groupId=parseInt($(this).attr('data-id')); 
					$("#dailyGroupIds").val(parseInt($(this).attr('data-id')));
				}
				$scope.$apply();
 				return false;
			});
			$element.bind('mouseleave',function(){ 
				$("#dailyGroupInputText").val(b.find('a.cur').text());
				b.find('.ui_s_t').blur();
				b.hide();
//				$scope.$apply();
				return false;
			});
		}
	}
});
app.directive('agSelectw', function($http){
	return { 
		restrict:'AE',
		scope:{ 
			groupId:'=groupId'
		},
		template:'<div class="ui_select"><input type="text" id="weeklyGroupInputText" class="ui_s_t" ng-bind="stxt" ng-model="stxt" /><div class="ui_s_bk"><a href="javascript:void(0)" ng-repeat="key in dataGroupSelects | filter: {groupName:stxt}" ng-bind="key.groupName" data-id="{{key.groupId}}"></a></div></div>',
		controller:function($scope,$element,$attrs){ 
			var a = $element.find('.ui_s_t'),
				b = $element.find('.ui_s_bk'),
				c = $attrs.ajaxs,
				d = $attrs.loadajaxs;
			$element.children('.ui_select').css('zIndex',101);
			$element.bind('click', function() {
				b.slideDown();
			});
			$element.bind('mouseleave', function() {
				if(a.text()!=""){
					b.hide();
				}
			});
		},
		link:function($scope,$element,$attrs){ 
			var a=$element.find('.ui_s_t'),
				b=$element.find('.ui_s_bk'),
				c=$attrs.ajaxs;
			
			if(c!=undefined){
				$http.get(c+'?vtimes='+new Date().getTime()).success(function(data){ 
					$scope.dataGroupSelects=data.returnData;
				});
			}			
			a.click(function() {
				$scope.stxt=undefined;
				$scope.$apply();
			});
			b.find('a').live("click",function(){ 
				b.hide();
				$(this).addClass('cur').siblings().removeClass('cur');
				a.val($(this).text());
				$scope.stxt=$(this).text();
				if($attrs.groupId!=undefined) {
					$scope.groupId=parseInt($(this).attr('data-id')); 
					$("#weeklyGroupIds").val(parseInt($(this).attr('data-id')));
				}
				$scope.$apply();
 				return false;
			});
			$element.bind('mouseleave',function(){ 
				$("#weeklyGroupInputText").val(b.find('a.cur').text());
				b.find('.ui_s_t').blur();
				b.hide();
//				$scope.$apply();
				return false;
			});
		}
	}
});
app.directive('agSelectm', function($http){
	return { 
		restrict:'AE',
		scope:{ 
			groupId:'=groupId'
		},
		template:'<div class="ui_select"><input type="text" id="monthlyGroupInputText" class="ui_s_t" ng-bind="stxt" ng-model="stxt" /><div class="ui_s_bk"><a href="javascript:void(0)" ng-repeat="key in dataGroupSelects | filter: {groupName:stxt}" ng-bind="key.groupName" data-id="{{key.groupId}}"></a></div></div>',
		controller:function($scope,$element,$attrs){ 
			var a = $element.find('.ui_s_t'),
				b = $element.find('.ui_s_bk'),
				c = $attrs.ajaxs,
				d = $attrs.loadajaxs;
			$element.children('.ui_select').css('zIndex',100);
			$element.bind('click', function() {
				b.slideDown();
			});
			$element.bind('mouseleave', function() {
				if(a.text()!=""){
					b.hide();
				}
			});
		},
		link:function($scope,$element,$attrs){ 
			var a=$element.find('.ui_s_t'),
				b=$element.find('.ui_s_bk'),
				c=$attrs.ajaxs;
			
			if(c!=undefined){
				$http.get(c+'?vtimes='+new Date().getTime()).success(function(data){ 
					$scope.dataGroupSelects=data.returnData;
				});
			}			
			a.click(function() {
				$scope.stxt=undefined;
				$scope.$apply();
			});
			b.find('a').live("click",function(){ 
				b.hide();
				$(this).addClass('cur').siblings().removeClass('cur');
				a.val($(this).text());
				$scope.stxt=$(this).text();
				if($attrs.groupId!=undefined) {
					$scope.groupId=parseInt($(this).attr('data-id')); 
					$("#monthlyGroupIds").val(parseInt($(this).attr('data-id')));
				}
				$scope.$apply();
 				return false;
			});
			$element.bind('mouseleave',function(){ 
				$("#monthlyGroupInputText").val(b.find('a.cur').text());
				b.find('.ui_s_t').blur();
				b.hide();
//				$scope.$apply();
				return false;
			});
		}
	}
});